package com.example.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author 二七和夭夭
 * @email 2646498606@qq.com
 * @date 2022/8/24 17:13
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //未登录导致的异常
    @ExceptionHandler(ClassCastException.class)
    public ModelAndView loginException( ClassCastException e) {
        if (e.getMessage().contains("com.example.pojo.User")) {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("redirect:/jsp/login.jsp");
            return modelAndView;
        }
        return null;
    }
}
